/*****************************************************************************
* File Name: motion_sensor_hal.c
*
* Version: 1.0
*
* Description:
* This file contains API for motion sensor
*
* Note:
* Define DISABLE_MOTION_SENSOR in platform.h to disable motion sensor module
*
* Owner: KMVP
*
* Related Document:
* SCB component datasheet
*
* Hardware Dependency:
* MPU6500 Datasheet
*
* Code Tested With:
* 1. PSoC Creator 3.1 
* 2. ARM GCC 4.8.4
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#include "motion_sensor_hal.h"
#ifndef DISABLE_MOTION_SENSOR

/* Variable to store the timer callback function */
static MotionSensorCBK motionSensorCallBack = NULL;

/*****************************************************************************
* Function Name: Motion_Sensor_Hal_CallBack()
******************************************************************************
* Summary:
* This function is used to provide a callback to the motion sensor interrupt
*
* Parameters:
* None
*
* Return:
* None
*
* Theory:
*
* Side Effects:
* None
*
* Note:
* None
*****************************************************************************/
static void Motion_Sensor_Hal_CallBack(void)
{
    if(motionSensorCallBack != NULL)
    {
        motionSensorCallBack();
    }
}

/*****************************************************************************
* Function Name: Motion_Sensor_Hal_Init()
******************************************************************************
* Summary:
* This function is used to initlize the motion sensor by calling the reseting 
* Nthe motion sensor and calling motion sensor driver
* and reseting the device.
*
* Parameters:
* MotionSensorCBK cbk - callback function that need to be registered 
*
* Return:
* None
*
* Theory:
* None
*
* Side Effects:
* None 
*
* Note:
* None
*****************************************************************************/
void Motion_Sensor_Hal_Init(MotionSensorCBK cbk)
{
    motionSensorCallBack = cbk;
    Motion_Sensor_Interrupt_StartEx(Motion_Sensor_Hal_CallBack);

    Motion_Sensor_I2C_Start();

    /* Explicit reset is essential as device reset is not propagated to Motion Sensor in hardware */
    MPU6500_Reset();
    MPU6500_Start();
}

/*****************************************************************************
* Function Name: Motion_Sensor_Hal_GetGyroData()
******************************************************************************
* Summary:
* This function is used to collect the gyro data and pnimpulate the data to follow
* the data processing  conventions.
*
* Parameters:
* int16 *buffer - buffer pointer where the gyro data is filled
*
* Return:
* None
*
* Theory:
* Depending on mounting of the motion sensor on device PCB, the axis orientations
* may be different than what is expected by the processing library. Thus the data
* from the sensor should be manipulated in this function.
*
* Side Effects:
* None 
*
* Note:
* The buffer pointer should be at least 6 bytes
*****************************************************************************/
void Motion_Sensor_Hal_GetGyroData(int16 *buffer)
{
   if(buffer != NULL)
    {
        MPU6500_GYRO_GetSample(buffer);
    }
}

/*****************************************************************************
* Function Name: Motion_Sensor_Hal_GetAccData()
******************************************************************************
* Summary:
* This function is used to collect the accelerometer data and manimpulate the
* data to follow the data processing  conventions.
*
* Parameters:
* int16 *buffer - buffer pointer where the gyro data is filled
*
* Return:
* None
*
* Theory:
* Depending on mounting of the motion sensor on device PCB, the axis orientations
* may be different than what is expected by the processing library. Thus the data
* from the sensor should be manipulated in this function.
*
* Side Effects:
* None
*
* Note:
* None
*****************************************************************************/
void Motion_Sensor_Hal_GetAccData(int16 *buffer)
{
    if(buffer != NULL)
    {
        MPU6500_ACC_GetSample(buffer);
        /* PCB mounting of the motion sensor inverts the direction of axes of the sensor.
         * Hence compensate them with arithmetic negation
         */
        buffer[0] = -buffer[0];
        buffer[1] = -buffer[1];
        buffer[2] = -buffer[2];
    }
}

/*****************************************************************************
* Function Name: Motion_Sensor_Hal_Enable_LowPowerMode()
******************************************************************************
* Summary:
* This function is used to put motion sensor in low power mode 
*
* Parameters:
* None
*
* Return:
* None
*
* Theory:
* This function enables the motion sensor low power mode by disabling gyroscope 
* and enabling accelerometer in motion detect mode
*
* Side Effects:
* None
*
* Note:
* None
*****************************************************************************/
void Motion_Sensor_Hal_Enable_LowPowerMode(void)
{
    MPU6500_StartMotionDetection();
    Motion_Sensor_Interrupt_Enable();
}

/*****************************************************************************
* Function Name: Motion_Sensor_Hal_Disable_LowPowerMode()
******************************************************************************
* Summary:
* This function is disables motion detect low power mode of motion sensor and 
* restore default configuration of motion sensor
*
* Parameters:
* None
*
* Return:
* None
*
* Theory:
* None
*
* Side Effects:
* None
*
* Note:
* This function is supposed to be called after low power motion detection triggers
* callback event from motion sensor
*****************************************************************************/ 
void Motion_Sensor_Hal_Disable_LowPowerMode(void)
{
    MPU6500_StopMotionDetection(); 
    MPU6500_Start();
    Motion_Sensor_Interrupt_Disable();
}

#endif /* DISABLE_MOTION_SENSOR */
